#include "lh_toce.h"

TOUCH_EVE   TcEve;      



void TcEveInit(void)
{
    TcEve.NowEvent  = TOUCH_NOT;
    TcEve.LastEvent = TOUCH_NOT;
    TcEve.PrdX      = 0;
    TcEve.PrdY      = 0;
    TcEve.NowX      = 0;
    TcEve.NowY      = 0;
    TcEve.LastX     = 0;
    TcEve.LastY     = 0;
    TcEve.TocSyn    = TOC_SYN_NO;
}



void SetTocSyn(void)
{
    TcEve.TocSyn = TOC_SYN_YES;
}



void ClrTocSyn(void)
{
    TcEve.TocSyn = TOC_SYN_NO;
}



void TouchAD_Seq(LH_S16 *Data)
{
    LH_U8 i = 0, j = 0;
    LH_U16 Temp = 0;
    
    for(i = 0; i < 9; i++)
    {
        for(j = i+1; j<10; j++)
        {
            if(Data[i] > Data[j])
            {
                Temp    = Data[i];
                Data[i] = Data[j];
                Data[j] = Temp;
            }
        }
    }
}



LH_U8 TouchGet_XY(LH_U16 *X, LH_U16 *Y)
{
    LH_U8  Count = 0;
    LH_S16 X_DataBuffer[30] = {0},
           Y_DataBuffer[30] = {0};
    LH_S16 X_AD = 0;
    LH_S16 Y_AD = 0;
    static LH_U8 BtnPreDowFlag1 = 0;   
    static LH_U16 BtnPreDowCount1 = 0;
    static LH_S16 LastX = 0;
    static LH_S16 LastY = 0;
    static LH_S16 LastCoordX = 0;
    static LH_S16 LastCoordY = 0;

    X_AD = Touch_ReadX();
    Y_AD = Touch_ReadY();

    if ( (X_AD == 0) || (Y_AD == 0) )
    {
        BtnPreDowFlag1 = 0;
        BtnPreDowCount1 = 0;
        return TOUCH_GETN;      
    }

    if(BtnPreDowFlag1 == 0)
        BtnPreDowFlag1 = 1;     
    
    if((BtnPreDowFlag1 == 1) && (BtnPreDowCount1 < LH_BTN_DELAY))   
        BtnPreDowCount1 ++;
    if(BtnPreDowCount1 >= LH_BTN_DELAY)
    {
        do
        {
            X_DataBuffer[Count] = Touch_ReadX();
            Y_DataBuffer[Count] = Touch_ReadY(); 
            if (
#if (TOUCH_AD_X_LEFT > TOUCH_AD_X_RIGHT)
                (X_DataBuffer[Count] > TOUCH_AD_X_LEFT + TOUCH_OVER_VLU) || (X_DataBuffer[Count] < TOUCH_AD_X_RIGHT - TOUCH_OVER_VLU) 
#else
                (X_DataBuffer[Count] < TOUCH_AD_X_LEFT - TOUCH_OVER_VLU) || (X_DataBuffer[Count] > TOUCH_AD_X_RIGHT + TOUCH_OVER_VLU)
#endif
        
#if (TOUCH_AD_Y_TOP > TOUCH_AD_Y_DOWN)
             || (Y_DataBuffer[Count] > TOUCH_AD_Y_TOP + TOUCH_OVER_VLU)  || (Y_DataBuffer[Count] < TOUCH_AD_Y_DOWN - TOUCH_OVER_VLU) 
#else
             || (Y_DataBuffer[Count] < TOUCH_AD_Y_TOP - TOUCH_OVER_VLU)  || (Y_DataBuffer[Count] > TOUCH_AD_Y_DOWN + TOUCH_OVER_VLU) 
#endif
                )
            {




                break;
            }
            
            Count++; 
        }
        while(Count < 30);      
        
        if (Count == 30)
        {
            TouchAD_Seq(X_DataBuffer);  
            TouchAD_Seq(Y_DataBuffer);
            X_AD = (X_DataBuffer[3] + X_DataBuffer[4] + X_DataBuffer[5])/3;
            Y_AD = (Y_DataBuffer[3] + Y_DataBuffer[4] + Y_DataBuffer[5])/3;
            
            if(BtnPreDowCount1 == LH_BTN_DELAY)
            {
                LastX = X_AD;
                LastY = Y_AD;
            }            
            if((GetAbs(X_AD - LastX) > LH_SHAKE_VLU) || (GetAbs(Y_AD - LastY) > LH_SHAKE_VLU))
            {
                X_AD = LastX;
                Y_AD = LastY;
            }
            LastX = X_AD;
            LastY = Y_AD;
            
#if (TOUCH_AD_X_LEFT > TOUCH_AD_X_RIGHT)
            X_AD = (LH_U16)((LH_F32)GetAbs(TOUCH_AD_X_LEFT - X_AD)/((LH_F32)(TOUCH_AD_X_LEFT - TOUCH_AD_X_RIGHT)/(LH_F32)TFT_XSIZE));
#else
            X_AD = (LH_U16)((LH_F32)GetAbs(X_AD - TOUCH_AD_X_LEFT)/((LH_F32)(TOUCH_AD_X_RIGHT - TOUCH_AD_X_LEFT)/(LH_F32)TFT_XSIZE));
#endif
            if(BtnPreDowCount1 == LH_BTN_DELAY)
                LastCoordX = X_AD;
            if((GetAbs(X_AD - LastCoordX) < 3) || (X_AD >= TFT_XSIZE))   
                X_AD = LastCoordX;
            LastCoordX = X_AD;
            
#if (TOUCH_AD_Y_TOP > TOUCH_AD_Y_DOWN)
            Y_AD = (LH_U16)((LH_F32)GetAbs(TOUCH_AD_Y_TOP - Y_AD)/((LH_F32)(TOUCH_AD_Y_TOP - TOUCH_AD_Y_DOWN)/(LH_F32)TFT_YSIZE)); 
#else
            Y_AD = (LH_U16)((LH_F32)GetAbs(Y_AD - TOUCH_AD_Y_TOP)/((LH_F32)(TOUCH_AD_Y_DOWN - TOUCH_AD_Y_TOP)/(LH_F32)TFT_YSIZE)); 
#endif
            if(BtnPreDowCount1 == LH_BTN_DELAY)
                LastCoordY = Y_AD;
            if((GetAbs(Y_AD - LastCoordY) < 3) || (Y_AD >= TFT_YSIZE))   
                Y_AD = LastCoordY;
            LastCoordY = Y_AD;
            
            if (BtnPreDowCount1 == LH_BTN_DELAY)
            {
                BtnPreDowCount1 ++;
                
                TcEve.PrdX = X_AD;
                TcEve.PrdY = Y_AD;
            }
            *X = X_AD;
            *Y = Y_AD;
            return TOUCH_GETY;      
        }
        else
        {                           
            if(BtnPreDowCount1 == LH_BTN_DELAY)
                return TOUCH_GETN;  
            else
            {
                X_AD = LastX;
                Y_AD = LastY;
                
#if (TOUCH_AD_X_LEFT > TOUCH_AD_X_RIGHT)
                X_AD = (LH_U16)((LH_F32)GetAbs(TOUCH_AD_X_LEFT - X_AD)/((LH_F32)(TOUCH_AD_X_LEFT - TOUCH_AD_X_RIGHT)/(LH_F32)TFT_XSIZE));
#else
                X_AD = (LH_U16)((LH_F32)GetAbs(X_AD - TOUCH_AD_X_LEFT)/((LH_F32)(TOUCH_AD_X_RIGHT - TOUCH_AD_X_LEFT)/(LH_F32)TFT_XSIZE));
#endif
                if(BtnPreDowCount1 == LH_BTN_DELAY)
                LastCoordX = X_AD;
                if((GetAbs(X_AD - LastCoordX) < 3) || (X_AD >= TFT_XSIZE))   
                X_AD = LastCoordX;
                LastCoordX = X_AD;
            
#if (TOUCH_AD_Y_TOP > TOUCH_AD_Y_DOWN)
                Y_AD = (LH_U16)((LH_F32)GetAbs(TOUCH_AD_Y_TOP - Y_AD)/((LH_F32)(TOUCH_AD_Y_TOP - TOUCH_AD_Y_DOWN)/(LH_F32)TFT_YSIZE)); 
#else
                Y_AD = (LH_U16)((LH_F32)GetAbs(Y_AD - TOUCH_AD_Y_TOP)/((LH_F32)(TOUCH_AD_Y_DOWN - TOUCH_AD_Y_TOP)/(LH_F32)TFT_YSIZE)); 
#endif
                if(BtnPreDowCount1 == LH_BTN_DELAY)
                    LastCoordY = Y_AD;
                if((GetAbs(Y_AD - LastCoordY) < 3) || (Y_AD >= TFT_YSIZE))   
                    Y_AD = LastCoordY;
                LastCoordY = Y_AD;
                
                *X = X_AD;
                *Y = Y_AD;
                return TOUCH_GETY;  
            }
        }
    }
    else
        return TOUCH_GETN;          
}



void UpDataTcEve(void)
{
    if (TcEve.TocSyn == TOC_SYN_NO)
    {
        if (TouchGet_XY(&TcEve.NowX, &TcEve.NowY) == TOUCH_GETN)  
        {
            
            if ( (TcEve.LastEvent == TOUCH_START) || (TcEve.LastEvent == TOUCH_CATCH) )      
            {
                TcEve.NowEvent = TOUCH_STOP;        
                TcEve.LastEvent = TcEve.NowEvent;   
            }
            
            else if( (TcEve.LastEvent == TOUCH_STOP) || (TcEve.LastEvent == TOUCH_NOT) )
            {
                TcEve.NowEvent = TOUCH_NOT;         
                TcEve.LastEvent = TcEve.NowEvent;   
            }
        }
        else
        {
            
            if ( (TcEve.LastEvent == TOUCH_STOP) || (TcEve.LastEvent == TOUCH_NOT) )   
            {        
                
                TcEve.NowEvent = TOUCH_START;           
                TcEve.LastEvent = TcEve.NowEvent;       
            }
            
            else if ( (TcEve.LastEvent == TOUCH_START) || (TcEve.LastEvent == TOUCH_CATCH) ) 
            {
                TcEve.NowEvent = TOUCH_CATCH;           
                TcEve.LastEvent = TcEve.NowEvent;       
            }       
            TcEve.LastX = TcEve.NowX;                   
            TcEve.LastY = TcEve.NowY;                   
        }
        
        SetTocSyn();
    }
}


void TouchTest(void)
{
    static u16 i1 = 0;
    i1 ++;
    
    ClrTocSyn();
    UpDataTcEve();
    
    SetTftPutBack();
    if(TcEve.NowEvent == TOUCH_START)
    {
        TFT_String(0,140, (s8 *)"TOUCH_START   ", 4000);
        TFT_String(0,160, (s8 *)"              ", 4000);
        TFT_String(0,180, (s8 *)"              ", 4000);
        TFT_String(0,200, (s8 *)"              ", 4000);
    }
    if(TcEve.NowEvent == TOUCH_CATCH)
    {
        TFT_String(0,140, (s8 *)"              ", 4000);
        TFT_String(0,160, (s8 *)"TOUCH_CATCH   ", 4000);
        TFT_String(0,180, (s8 *)"              ", 4000);
        TFT_String(0,200, (s8 *)"              ", 4000);
    }
    if(TcEve.NowEvent == TOUCH_STOP)
    {
        TFT_String(0,140, (s8 *)"              ", 4000);
        TFT_String(0,160, (s8 *)"              ", 4000);
        TFT_String(0,180, (s8 *)"TOUCH_STOP    ", 4000);
        TFT_String(0,200, (s8 *)"              ", 4000);
    }
    if(TcEve.NowEvent == TOUCH_NOT)
    {
        TFT_String(0,140, (s8 *)"              ", 4000);
        TFT_String(0,160, (s8 *)"              ", 4000);
        TFT_String(0,180, (s8 *)"              ", 4000);
        TFT_String(0,200, (s8 *)"TOUCH_NOT     ", 4000);
    }
    
    TFT_Dec(0,20, TcEve.PrdX, 4, 6000);
    TFT_Dec(0,40, TcEve.PrdY, 4, 6000);
    TFT_Dec(0,60, TcEve.NowX, 4, 6000);
    TFT_Dec(0,80, TcEve.NowY, 4, 6000);
    TFT_Dec(0,100, Touch_ReadX(), 4, 6000);
    TFT_Dec(0,120, Touch_ReadY(), 4, 6000);
    TFT_Dec(0,0, i1, 4, 6000);
    
    TFT_SetPoint(TcEve.NowX, TcEve.NowY, 0xf800);
    ClrTftPutBack();
}





